Skip to contentSkip to conversation

Change in openvpn[master]: Implement methods to generate and manage OpenVPN Epoch keys

Openvpn-devel In review plaisthos (Code Review) 2 participants 14 messages Nov 11, 2024 – Jan 10, 2025 1 message not in the archive

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): gerrit.1731290343000.Id7d6a576ca8c9560cb2dfae82fc62175820e9b80@...2715...

#1 lore raw

Attention is currently required from: flichtenheld.

Hello flichtenheld,

I'd like you to do a code review.
Please visit

    http://gerrit.openvpn.net/c/openvpn/+/804?usp=email

to review the following change.

Change subject: Implement methods to generate and manage OpenVPN Epoch keys
......................................................................

Implement methods to generate and manage OpenVPN Epoch keys

This implements functions that allow these keys to be generated and managed. It does not yet implement using them for the data channel.

Change-Id Id7d6a576ca8c9560cb2dfae82fc62175820e9b80

---
M src/openvpn/crypto.c
M src/openvpn/crypto.h
M src/openvpn/crypto_epoch.c
M src/openvpn/crypto_epoch.h
M src/openvpn/packet_id.c
M src/openvpn/packet_id.h
M src/openvpn/ssl_common.h
M tests/unit_tests/openvpn/test_crypto.c

8 files changed, 776 insertions(+), 22 deletions(-)

git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/04/804/1

src/openvpn/crypto.c +20 -3
index f4453d8..dc23ffc 100644
@@ -909,14 +909,27 @@
     if (cipher_ctx_mode_aead(ctx->cipher))
     {
         size_t impl_iv_len = 0;
+        size_t impl_iv_offset = 0;
         ASSERT(cipher_ctx_iv_length(ctx->cipher) >= OPENVPN_AEAD_MIN_IV_LEN);
-        impl_iv_len = cipher_ctx_iv_length(ctx->cipher) - sizeof(packet_id_type);
+
+        /* Epoch keys use XOR of full IV length with the packet id to generate
+         * IVs. Old data format uses concatenation instead (XOR with 0 for the
+         * first 4 bytes (sizeof(packet_id_type) */
+        if (key->epoch)
+        {
+            impl_iv_len = cipher_ctx_iv_length(ctx->cipher);
+            impl_iv_offset = 0;
+        }
+        else
+        {
+            impl_iv_len = cipher_ctx_iv_length(ctx->cipher) - sizeof(packet_id_type);
+            impl_iv_offset = sizeof(packet_id_type);
+        }
         ASSERT(impl_iv_len <= OPENVPN_MAX_IV_LENGTH);
         ASSERT(impl_iv_len <= MAX_HMAC_KEY_LENGTH);
         ASSERT(key->hmac_size >= impl_iv_len);
         CLEAR(ctx->implicit_iv);
-        /* The first bytes of the IV are filled with the packet id */
-        memcpy(ctx->implicit_iv + sizeof(packet_id_type), key->hmac, impl_iv_len);
+        memcpy(ctx->implicit_iv + impl_iv_offset, key->hmac, impl_iv_len);
     }
 }
 
@@ -965,6 +978,7 @@
              hmac_ctx_size(ctx->hmac));
 
     }
+    ctx->epoch = key->epoch;
     gc_free(&gc);
 }
 
@@ -977,6 +991,7 @@
     snprintf(log_prefix, sizeof(log_prefix), "Outgoing %s", name);
     init_key_ctx(ctx, key_params, kt, OPENVPN_OP_ENCRYPT, log_prefix);
     key_ctx_update_implicit_iv(ctx, key_params);
+    ctx->epoch = key_params->epoch;
 }
 
 void
@@ -988,6 +1003,7 @@
     snprintf(log_prefix, sizeof(log_prefix), "Incoming %s", name);
     init_key_ctx(ctx, key_params, kt, OPENVPN_OP_DECRYPT, log_prefix);
     key_ctx_update_implicit_iv(ctx, key_params);
+    ctx->epoch = key_params->epoch;
 }
 
 void
@@ -1025,6 +1041,7 @@
     }
     CLEAR(ctx->implicit_iv);
     ctx->plaintext_blocks = 0;
+    ctx->epoch = 0;
 }
 
 void
src/openvpn/crypto.h +49 -2

93 lines not on this page · raw message

src/openvpn/crypto_epoch.c +337 -2

356 lines not on this page · raw message

src/openvpn/crypto_epoch.h +78 -1

82 lines not on this page · raw message

src/openvpn/packet_id.c +34 -11

57 lines not on this page · raw message

src/openvpn/packet_id.h +9 -0
index 79224d2..7620ce5 100644
@@ -203,6 +203,15 @@
 
 void packet_id_free(struct packet_id *p);
 
+/**
+ * Move the packet id recv structure from src to dest. src will will
+ * be reinitialised with
+ * @param dest
+ * @param src
+ */
+void
+packet_id_move_recv(struct packet_id_rec *dest, struct packet_id_rec *src);
+
 /* should we accept an incoming packet id ? */
 bool packet_id_test(struct packet_id_rec *p,
                     const struct packet_id_net *pin);
src/openvpn/ssl_common.h +2 -2
index ce1cd9e..511127d 100644
@@ -333,8 +333,8 @@
     interval_t packet_timeout;
     int64_t renegotiate_bytes;
     int64_t renegotiate_packets;
-    /** This limit for AEAD cipher, this is the sum of packets + blocks
-     * that are allowed to be used */
+    /** This limit for AEAD cipher when not running in epoch data key mode,
+     * this is the sum of packets + blocks that are allowed to be used */
     int64_t aead_usage_limit;
     interval_t renegotiate_seconds;
 
tests/unit_tests/openvpn/test_crypto.c +247 -1

269 lines not on this page · raw message

--
To view, visit http://gerrit.openvpn.net/c/openvpn/+/804?usp=email
To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings

Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: Id7d6a576ca8c9560cb2dfae82fc62175820e9b80
Gerrit-Change-Number: 804
Gerrit-PatchSet: 1
Gerrit-Owner: plaisthos <[email protected]...>
Gerrit-Reviewer: flichtenheld <[email protected]...>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-Attention: flichtenheld <[email protected]...>
Gerrit-MessageType: newchange

#2 lore raw
In reply to a message not in the archive

Attention is currently required from: flichtenheld.

Hello flichtenheld,

I'd like you to reexamine a change. Please visit

    http://gerrit.openvpn.net/c/openvpn/+/804?usp=email

to look at the new patch set (#4).

Change subject: Implement methods to generate and manage OpenVPN Epoch keys
......................................................................

Implement methods to generate and manage OpenVPN Epoch keys

This implements functions that allow these keys to be generated and managed. It does not yet implement using them for the data channel.

Change-Id Id7d6a576ca8c9560cb2dfae82fc62175820e9b80

---
M src/openvpn/crypto.c
M src/openvpn/crypto.h
M src/openvpn/crypto_epoch.c
M src/openvpn/crypto_epoch.h
M src/openvpn/packet_id.c
M src/openvpn/packet_id.h
M src/openvpn/ssl_common.h
M tests/unit_tests/openvpn/test_crypto.c

8 files changed, 774 insertions(+), 20 deletions(-)

git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/04/804/4

src/openvpn/crypto.c +20 -3
index 769a274..a055b20 100644
@@ -908,14 +908,27 @@
     if (cipher_ctx_mode_aead(ctx->cipher))
     {
         size_t impl_iv_len = 0;
+        size_t impl_iv_offset = 0;
         ASSERT(cipher_ctx_iv_length(ctx->cipher) >= OPENVPN_AEAD_MIN_IV_LEN);
-        impl_iv_len = cipher_ctx_iv_length(ctx->cipher) - sizeof(packet_id_type);
+
+        /* Epoch keys use XOR of full IV length with the packet id to generate
+         * IVs. Old data format uses concatenation instead (XOR with 0 for the
+         * first 4 bytes (sizeof(packet_id_type) */
+        if (key->epoch)
+        {
+            impl_iv_len = cipher_ctx_iv_length(ctx->cipher);
+            impl_iv_offset = 0;
+        }
+        else
+        {
+            impl_iv_len = cipher_ctx_iv_length(ctx->cipher) - sizeof(packet_id_type);
+            impl_iv_offset = sizeof(packet_id_type);
+        }
         ASSERT(impl_iv_len <= OPENVPN_MAX_IV_LENGTH);
         ASSERT(impl_iv_len <= MAX_HMAC_KEY_LENGTH);
         ASSERT(key->hmac_size >= impl_iv_len);
         CLEAR(ctx->implicit_iv);
-        /* The first bytes of the IV are filled with the packet id */
-        memcpy(ctx->implicit_iv + sizeof(packet_id_type), key->hmac, impl_iv_len);
+        memcpy(ctx->implicit_iv + impl_iv_offset, key->hmac, impl_iv_len);
     }
 }
 
@@ -964,6 +977,7 @@
              hmac_ctx_size(ctx->hmac));
 
     }
+    ctx->epoch = key->epoch;
     gc_free(&gc);
 }
 
@@ -976,6 +990,7 @@
     snprintf(log_prefix, sizeof(log_prefix), "Outgoing %s", name);
     init_key_ctx(ctx, key_params, kt, OPENVPN_OP_ENCRYPT, log_prefix);
     key_ctx_update_implicit_iv(ctx, key_params);
+    ctx->epoch = key_params->epoch;
 }
 
 void
@@ -987,6 +1002,7 @@
     snprintf(log_prefix, sizeof(log_prefix), "Incoming %s", name);
     init_key_ctx(ctx, key_params, kt, OPENVPN_OP_DECRYPT, log_prefix);
     key_ctx_update_implicit_iv(ctx, key_params);
+    ctx->epoch = key_params->epoch;
 }
 
 void
@@ -1024,6 +1040,7 @@
     }
     CLEAR(ctx->implicit_iv);
     ctx->plaintext_blocks = 0;
+    ctx->epoch = 0;
 }
 
 void
src/openvpn/crypto.h +49 -2

93 lines not on this page · raw message

src/openvpn/crypto_epoch.c +335 -0

345 lines not on this page · raw message

src/openvpn/crypto_epoch.h +78 -1

82 lines not on this page · raw message

src/openvpn/packet_id.c +34 -11

57 lines not on this page · raw message

src/openvpn/packet_id.h +9 -0
index 79224d2..7620ce5 100644
@@ -203,6 +203,15 @@
 
 void packet_id_free(struct packet_id *p);
 
+/**
+ * Move the packet id recv structure from src to dest. src will will
+ * be reinitialised with
+ * @param dest
+ * @param src
+ */
+void
+packet_id_move_recv(struct packet_id_rec *dest, struct packet_id_rec *src);
+
 /* should we accept an incoming packet id ? */
 bool packet_id_test(struct packet_id_rec *p,
                     const struct packet_id_net *pin);
src/openvpn/ssl_common.h +2 -2
index ce1cd9e..511127d 100644
@@ -333,8 +333,8 @@
     interval_t packet_timeout;
     int64_t renegotiate_bytes;
     int64_t renegotiate_packets;
-    /** This limit for AEAD cipher, this is the sum of packets + blocks
-     * that are allowed to be used */
+    /** This limit for AEAD cipher when not running in epoch data key mode,
+     * this is the sum of packets + blocks that are allowed to be used */
     int64_t aead_usage_limit;
     interval_t renegotiate_seconds;
 
tests/unit_tests/openvpn/test_crypto.c +247 -1

269 lines not on this page · raw message

--
To view, visit http://gerrit.openvpn.net/c/openvpn/+/804?usp=email
To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings

Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: Id7d6a576ca8c9560cb2dfae82fc62175820e9b80
Gerrit-Change-Number: 804
Gerrit-PatchSet: 4
Gerrit-Owner: plaisthos <[email protected]...>
Gerrit-Reviewer: flichtenheld <[email protected]...>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-Attention: flichtenheld <[email protected]...>
Gerrit-MessageType: newpatchset

#3 lore raw
In reply to a message not in the archive

Attention is currently required from: flichtenheld.

Hello flichtenheld,

I'd like you to reexamine a change. Please visit

    http://gerrit.openvpn.net/c/openvpn/+/804?usp=email

to look at the new patch set (#5).

Change subject: Implement methods to generate and manage OpenVPN Epoch keys
......................................................................

Implement methods to generate and manage OpenVPN Epoch keys

This implements functions that allow these keys to be generated and managed. It does not yet implement using them for the data channel.

Change-Id Id7d6a576ca8c9560cb2dfae82fc62175820e9b80

---
M src/openvpn/crypto.c
M src/openvpn/crypto.h
M src/openvpn/crypto_epoch.c
M src/openvpn/crypto_epoch.h
M src/openvpn/packet_id.c
M src/openvpn/packet_id.h
M src/openvpn/ssl_common.h
M tests/unit_tests/openvpn/test_crypto.c

8 files changed, 775 insertions(+), 22 deletions(-)

git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/04/804/5

src/openvpn/crypto.c +20 -3
index 769a274..a055b20 100644
@@ -908,14 +908,27 @@
     if (cipher_ctx_mode_aead(ctx->cipher))
     {
         size_t impl_iv_len = 0;
+        size_t impl_iv_offset = 0;
         ASSERT(cipher_ctx_iv_length(ctx->cipher) >= OPENVPN_AEAD_MIN_IV_LEN);
-        impl_iv_len = cipher_ctx_iv_length(ctx->cipher) - sizeof(packet_id_type);
+
+        /* Epoch keys use XOR of full IV length with the packet id to generate
+         * IVs. Old data format uses concatenation instead (XOR with 0 for the
+         * first 4 bytes (sizeof(packet_id_type) */
+        if (key->epoch)
+        {
+            impl_iv_len = cipher_ctx_iv_length(ctx->cipher);
+            impl_iv_offset = 0;
+        }
+        else
+        {
+            impl_iv_len = cipher_ctx_iv_length(ctx->cipher) - sizeof(packet_id_type);
+            impl_iv_offset = sizeof(packet_id_type);
+        }
         ASSERT(impl_iv_len <= OPENVPN_MAX_IV_LENGTH);
         ASSERT(impl_iv_len <= MAX_HMAC_KEY_LENGTH);
         ASSERT(key->hmac_size >= impl_iv_len);
         CLEAR(ctx->implicit_iv);
-        /* The first bytes of the IV are filled with the packet id */
-        memcpy(ctx->implicit_iv + sizeof(packet_id_type), key->hmac, impl_iv_len);
+        memcpy(ctx->implicit_iv + impl_iv_offset, key->hmac, impl_iv_len);
     }
 }
 
@@ -964,6 +977,7 @@
              hmac_ctx_size(ctx->hmac));
 
     }
+    ctx->epoch = key->epoch;
     gc_free(&gc);
 }
 
@@ -976,6 +990,7 @@
     snprintf(log_prefix, sizeof(log_prefix), "Outgoing %s", name);
     init_key_ctx(ctx, key_params, kt, OPENVPN_OP_ENCRYPT, log_prefix);
     key_ctx_update_implicit_iv(ctx, key_params);
+    ctx->epoch = key_params->epoch;
 }
 
 void
@@ -987,6 +1002,7 @@
     snprintf(log_prefix, sizeof(log_prefix), "Incoming %s", name);
     init_key_ctx(ctx, key_params, kt, OPENVPN_OP_DECRYPT, log_prefix);
     key_ctx_update_implicit_iv(ctx, key_params);
+    ctx->epoch = key_params->epoch;
 }
 
 void
@@ -1024,6 +1040,7 @@
     }
     CLEAR(ctx->implicit_iv);
     ctx->plaintext_blocks = 0;
+    ctx->epoch = 0;
 }
 
 void
src/openvpn/crypto.h +49 -2

93 lines not on this page · raw message

src/openvpn/crypto_epoch.c +335 -0

345 lines not on this page · raw message

src/openvpn/crypto_epoch.h +78 -1

82 lines not on this page · raw message

src/openvpn/packet_id.c +34 -11

57 lines not on this page · raw message

src/openvpn/packet_id.h +9 -0
index 79224d2..7620ce5 100644
@@ -203,6 +203,15 @@
 
 void packet_id_free(struct packet_id *p);
 
+/**
+ * Move the packet id recv structure from src to dest. src will will
+ * be reinitialised with
+ * @param dest
+ * @param src
+ */
+void
+packet_id_move_recv(struct packet_id_rec *dest, struct packet_id_rec *src);
+
 /* should we accept an incoming packet id ? */
 bool packet_id_test(struct packet_id_rec *p,
                     const struct packet_id_net *pin);
src/openvpn/ssl_common.h +2 -2
index ce1cd9e..511127d 100644
@@ -333,8 +333,8 @@
     interval_t packet_timeout;
     int64_t renegotiate_bytes;
     int64_t renegotiate_packets;
-    /** This limit for AEAD cipher, this is the sum of packets + blocks
-     * that are allowed to be used */
+    /** This limit for AEAD cipher when not running in epoch data key mode,
+     * this is the sum of packets + blocks that are allowed to be used */
     int64_t aead_usage_limit;
     interval_t renegotiate_seconds;
 
tests/unit_tests/openvpn/test_crypto.c +248 -3

272 lines not on this page · raw message

--
To view, visit http://gerrit.openvpn.net/c/openvpn/+/804?usp=email
To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings

Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: Id7d6a576ca8c9560cb2dfae82fc62175820e9b80
Gerrit-Change-Number: 804
Gerrit-PatchSet: 5
Gerrit-Owner: plaisthos <[email protected]...>
Gerrit-Reviewer: flichtenheld <[email protected]...>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-Attention: flichtenheld <[email protected]...>
Gerrit-MessageType: newpatchset

#4 lore raw
In reply to a message not in the archive

Attention is currently required from: flichtenheld.

Hello flichtenheld,

I'd like you to reexamine a change. Please visit

    http://gerrit.openvpn.net/c/openvpn/+/804?usp=email

to look at the new patch set (#6).

Change subject: Implement methods to generate and manage OpenVPN Epoch keys
......................................................................

Implement methods to generate and manage OpenVPN Epoch keys

This implements functions that allow these keys to be generated and managed. It does not yet implement using them for the data channel.

Change-Id Id7d6a576ca8c9560cb2dfae82fc62175820e9b80

---
M src/openvpn/crypto.c
M src/openvpn/crypto.h
M src/openvpn/crypto_epoch.c
M src/openvpn/crypto_epoch.h
M src/openvpn/packet_id.c
M src/openvpn/packet_id.h
M src/openvpn/ssl_common.h
M tests/unit_tests/openvpn/test_crypto.c

8 files changed, 774 insertions(+), 21 deletions(-)

git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/04/804/6

src/openvpn/crypto.c +20 -3
index 4b16630..4d6d83a 100644
@@ -909,14 +909,27 @@
     if (cipher_ctx_mode_aead(ctx->cipher))
     {
         size_t impl_iv_len = 0;
+        size_t impl_iv_offset = 0;
         ASSERT(cipher_ctx_iv_length(ctx->cipher) >= OPENVPN_AEAD_MIN_IV_LEN);
-        impl_iv_len = cipher_ctx_iv_length(ctx->cipher) - sizeof(packet_id_type);
+
+        /* Epoch keys use XOR of full IV length with the packet id to generate
+         * IVs. Old data format uses concatenation instead (XOR with 0 for the
+         * first 4 bytes (sizeof(packet_id_type) */
+        if (key->epoch)
+        {
+            impl_iv_len = cipher_ctx_iv_length(ctx->cipher);
+            impl_iv_offset = 0;
+        }
+        else
+        {
+            impl_iv_len = cipher_ctx_iv_length(ctx->cipher) - sizeof(packet_id_type);
+            impl_iv_offset = sizeof(packet_id_type);
+        }
         ASSERT(impl_iv_len <= OPENVPN_MAX_IV_LENGTH);
         ASSERT(impl_iv_len <= MAX_HMAC_KEY_LENGTH);
         ASSERT(impl_iv_len <= key->hmac_size);
         CLEAR(ctx->implicit_iv);
-        /* The first bytes of the IV are filled with the packet id */
-        memcpy(ctx->implicit_iv + sizeof(packet_id_type), key->hmac, impl_iv_len);
+        memcpy(ctx->implicit_iv + impl_iv_offset, key->hmac, impl_iv_len);
     }
 }
 
@@ -965,6 +978,7 @@
              hmac_ctx_size(ctx->hmac));
 
     }
+    ctx->epoch = key->epoch;
     gc_free(&gc);
 }
 
@@ -977,6 +991,7 @@
     snprintf(log_prefix, sizeof(log_prefix), "Outgoing %s", name);
     init_key_ctx(ctx, key_params, kt, OPENVPN_OP_ENCRYPT, log_prefix);
     key_ctx_update_implicit_iv(ctx, key_params);
+    ctx->epoch = key_params->epoch;
 }
 
 void
@@ -988,6 +1003,7 @@
     snprintf(log_prefix, sizeof(log_prefix), "Incoming %s", name);
     init_key_ctx(ctx, key_params, kt, OPENVPN_OP_DECRYPT, log_prefix);
     key_ctx_update_implicit_iv(ctx, key_params);
+    ctx->epoch = key_params->epoch;
 }
 
 void
@@ -1025,6 +1041,7 @@
     }
     CLEAR(ctx->implicit_iv);
     ctx->plaintext_blocks = 0;
+    ctx->epoch = 0;
 }
 
 void
src/openvpn/crypto.h +49 -2

93 lines not on this page · raw message

src/openvpn/crypto_epoch.c +335 -0

345 lines not on this page · raw message

src/openvpn/crypto_epoch.h +78 -1

82 lines not on this page · raw message

src/openvpn/packet_id.c +34 -11

57 lines not on this page · raw message

src/openvpn/packet_id.h +9 -0
index f6b1463..a6d49b6 100644
@@ -205,6 +205,15 @@
 
 void packet_id_free(struct packet_id *p);
 
+/**
+ * Move the packet id recv structure from src to dest. src will will
+ * be reinitialised with
+ * @param dest
+ * @param src
+ */
+void
+packet_id_move_recv(struct packet_id_rec *dest, struct packet_id_rec *src);
+
 /* should we accept an incoming packet id ? */
 bool packet_id_test(struct packet_id_rec *p,
                     const struct packet_id_net *pin);
src/openvpn/ssl_common.h +2 -2
index ced5299..77aef4f 100644
@@ -333,8 +333,8 @@
     interval_t packet_timeout;
     int64_t renegotiate_bytes;
     int64_t renegotiate_packets;
-    /** limit for AEAD cipher, this is the sum of packets + blocks
-     * that are allowed to be used */
+    /** limit for AEAD cipher when not running in epoch data key mode,
+     *  this is the sum of packets + blocks that are allowed to be used */
     int64_t aead_usage_limit;
     interval_t renegotiate_seconds;
 
tests/unit_tests/openvpn/test_crypto.c +247 -2

270 lines not on this page · raw message

--
To view, visit http://gerrit.openvpn.net/c/openvpn/+/804?usp=email
To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings

Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: Id7d6a576ca8c9560cb2dfae82fc62175820e9b80
Gerrit-Change-Number: 804
Gerrit-PatchSet: 6
Gerrit-Owner: plaisthos <[email protected]...>
Gerrit-Reviewer: flichtenheld <[email protected]...>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-Attention: flichtenheld <[email protected]...>
Gerrit-MessageType: newpatchset

#5 lore raw
In reply to a message not in the archive

Attention is currently required from: flichtenheld.

Hello flichtenheld,

I'd like you to reexamine a change. Please visit

    http://gerrit.openvpn.net/c/openvpn/+/804?usp=email

to look at the new patch set (#8).

Change subject: Implement methods to generate and manage OpenVPN Epoch keys
......................................................................

Implement methods to generate and manage OpenVPN Epoch keys

This implements functions that allow these keys to be generated and managed. It does not yet implement using them for the data channel.

Change-Id Id7d6a576ca8c9560cb2dfae82fc62175820e9b80

---
M src/openvpn/crypto.c
M src/openvpn/crypto.h
M src/openvpn/crypto_epoch.c
M src/openvpn/crypto_epoch.h
M src/openvpn/packet_id.c
M src/openvpn/packet_id.h
M src/openvpn/ssl_common.h
M tests/unit_tests/openvpn/test_crypto.c

8 files changed, 780 insertions(+), 22 deletions(-)

git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/04/804/8

src/openvpn/crypto.c +21 -4
index 6e442a3..4e93882 100644
@@ -918,14 +918,27 @@
     if (cipher_ctx_mode_aead(ctx->cipher))
     {
         size_t impl_iv_len = 0;
+        size_t impl_iv_offset = 0;
         ASSERT(cipher_ctx_iv_length(ctx->cipher) >= OPENVPN_AEAD_MIN_IV_LEN);
-        impl_iv_len = cipher_ctx_iv_length(ctx->cipher) - sizeof(packet_id_type);
-        ASSERT(impl_iv_len + sizeof(packet_id_type) <= OPENVPN_MAX_IV_LENGTH);
+
+        /* Epoch keys use XOR of full IV length with the packet id to generate
+         * IVs. Old data format uses concatenation instead (XOR with 0 for the
+         * first 4 bytes (sizeof(packet_id_type) */
+        if (key->epoch)
+        {
+            impl_iv_len = cipher_ctx_iv_length(ctx->cipher);
+            impl_iv_offset = 0;
+        }
+        else
+        {
+            impl_iv_len = cipher_ctx_iv_length(ctx->cipher) - sizeof(packet_id_type);
+            impl_iv_offset = sizeof(packet_id_type);
+        }
+        ASSERT(impl_iv_offset + impl_iv_len <= OPENVPN_MAX_IV_LENGTH);
         ASSERT(impl_iv_len <= MAX_HMAC_KEY_LENGTH);
         ASSERT(impl_iv_len <= key->hmac_size);
         CLEAR(ctx->implicit_iv);
-        /* The first bytes of the IV are filled with the packet id */
-        memcpy(ctx->implicit_iv + sizeof(packet_id_type), key->hmac, impl_iv_len);
+        memcpy(ctx->implicit_iv + impl_iv_offset, key->hmac, impl_iv_len);
     }
 }
 
@@ -974,6 +987,7 @@
              hmac_ctx_size(ctx->hmac));
 
     }
+    ctx->epoch = key->epoch;
     gc_free(&gc);
 }
 
@@ -986,6 +1000,7 @@
     snprintf(log_prefix, sizeof(log_prefix), "Outgoing %s", name);
     init_key_ctx(ctx, key_params, kt, OPENVPN_OP_ENCRYPT, log_prefix);
     key_ctx_update_implicit_iv(ctx, key_params);
+    ctx->epoch = key_params->epoch;
 }
 
 void
@@ -997,6 +1012,7 @@
     snprintf(log_prefix, sizeof(log_prefix), "Incoming %s", name);
     init_key_ctx(ctx, key_params, kt, OPENVPN_OP_DECRYPT, log_prefix);
     key_ctx_update_implicit_iv(ctx, key_params);
+    ctx->epoch = key_params->epoch;
 }
 
 void
@@ -1034,6 +1050,7 @@
     }
     CLEAR(ctx->implicit_iv);
     ctx->plaintext_blocks = 0;
+    ctx->epoch = 0;
 }
 
 void
src/openvpn/crypto.h +49 -2

93 lines not on this page · raw message

src/openvpn/crypto_epoch.c +340 -0

350 lines not on this page · raw message

src/openvpn/crypto_epoch.h +78 -1

82 lines not on this page · raw message

src/openvpn/packet_id.c +34 -11

57 lines not on this page · raw message

src/openvpn/packet_id.h +9 -0
index f6b1463..a6d49b6 100644
@@ -205,6 +205,15 @@
 
 void packet_id_free(struct packet_id *p);
 
+/**
+ * Move the packet id recv structure from src to dest. src will will
+ * be reinitialised with
+ * @param dest
+ * @param src
+ */
+void
+packet_id_move_recv(struct packet_id_rec *dest, struct packet_id_rec *src);
+
 /* should we accept an incoming packet id ? */
 bool packet_id_test(struct packet_id_rec *p,
                     const struct packet_id_net *pin);
src/openvpn/ssl_common.h +2 -2
index ccbc053..c7ae962 100644
@@ -333,8 +333,8 @@
     interval_t packet_timeout;
     int64_t renegotiate_bytes;
     int64_t renegotiate_packets;
-    /** limit for AEAD cipher, this is the sum of packets + blocks
-     * that are allowed to be used */
+    /** limit for AEAD cipher when not running in epoch data key mode,
+     *  this is the sum of packets + blocks that are allowed to be used */
     uint64_t aead_usage_limit;
     interval_t renegotiate_seconds;
 
tests/unit_tests/openvpn/test_crypto.c +247 -2

270 lines not on this page · raw message

--
To view, visit http://gerrit.openvpn.net/c/openvpn/+/804?usp=email
To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings

Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: Id7d6a576ca8c9560cb2dfae82fc62175820e9b80
Gerrit-Change-Number: 804
Gerrit-PatchSet: 8
Gerrit-Owner: plaisthos <[email protected]...>
Gerrit-Reviewer: flichtenheld <[email protected]...>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-Attention: flichtenheld <[email protected]...>
Gerrit-MessageType: newpatchset

#6 lore raw
In reply to a message not in the archive

Attention is currently required from: flichtenheld, plaisthos.

MaxF has posted comments on this change. ( http://gerrit.openvpn.net/c/openvpn/+/804?usp=email )

Change subject: Implement methods to generate and manage OpenVPN Epoch keys
......................................................................

Patch Set 9:

(12 comments)

Patchset:

PS9:
Reviewed until crypto_epoch.h, will continue later

File src/openvpn/crypto.h:

http://gerrit.openvpn.net/c/openvpn/+/804/comment/a432af93_97a33d7a :
PS9, Line 175: /** the epoch of the key is if it was generated as epoch data key material */ I think you mean: The epoch of the key, if it was generated as epoch data key material.

(Implying, it's undefined if this is a different kind of key?)

http://gerrit.openvpn.net/c/openvpn/+/804/comment/735a8e52_5784bd7e :
PS9, Line 223: is
this?

http://gerrit.openvpn.net/c/openvpn/+/804/comment/a6671a3f_87bca2ec :
PS9, Line 315: key
keys

http://gerrit.openvpn.net/c/openvpn/+/804/comment/5f52e18e_e35d296c :
PS9, Line 316: to switch also sending key
to also switch the sending key?

http://gerrit.openvpn.net/c/openvpn/+/804/comment/68672759_37e827d3 :
PS9, Line 324: switch
switched?

http://gerrit.openvpn.net/c/openvpn/+/804/comment/7c80d6b3_104e8e59 :
PS9, Line 524: @param epoch Epoch read from packet or 0 when epoch is not used. That's not an argument of the function.

File src/openvpn/crypto_epoch.h:

http://gerrit.openvpn.net/c/openvpn/+/804/comment/ced0bc22_010e9c3f :
PS9, Line 72: * @param epoch_key Epoch key to be used
: * @param key Destination for the generated data key
wrong order compared to the function.

http://gerrit.openvpn.net/c/openvpn/+/804/comment/cb952e40_38a4c3a6 :
PS9, Line 91: using
uses

http://gerrit.openvpn.net/c/openvpn/+/804/comment/480f9270_6b023e63 :
PS9, Line 96: a
for the receive key?

http://gerrit.openvpn.net/c/openvpn/+/804/comment/3dba7c21_2b0cdd56 :
PS9, Line 103: send_epoch_keyt
send_epoch_key

http://gerrit.openvpn.net/c/openvpn/+/804/comment/44263987_947e608e :
PS9, Line 118: * @param e1_send The E1 send epoch key derived by TLS-EKM
: * @param e1_recv The E1 receive epoch key derived by TLS-EKM
incomplete parameters

--
To view, visit http://gerrit.openvpn.net/c/openvpn/+/804?usp=email
To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings

Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: Id7d6a576ca8c9560cb2dfae82fc62175820e9b80
Gerrit-Change-Number: 804
Gerrit-PatchSet: 9
Gerrit-Owner: plaisthos <[email protected]...>
Gerrit-Reviewer: flichtenheld <[email protected]...>
Gerrit-CC: MaxF <[email protected]...>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-Attention: plaisthos <[email protected]...>
Gerrit-Attention: flichtenheld <[email protected]...>
Gerrit-Comment-Date: Sat, 28 Dec 2024 17:34:37 +0000
Gerrit-HasComments: Yes
Gerrit-Has-Labels: No
Gerrit-MessageType: comment

#7 lore raw
In reply to a message not in the archive

Attention is currently required from: flichtenheld, plaisthos.

MaxF has posted comments on this change. ( http://gerrit.openvpn.net/c/openvpn/+/804?usp=email )

Change subject: Implement methods to generate and manage OpenVPN Epoch keys
......................................................................

Patch Set 9: Code-Review-1

(13 comments)

Patchset:

PS9:
Needs uncrustification.

Code looks good to me, I just have some comments about comments and naming.

File src/openvpn/crypto.h:

http://gerrit.openvpn.net/c/openvpn/+/804/comment/71ce8066_a53680c4 :
PS9, Line 307: This limit for AEAD cipher
The limit for AEAD ciphers?

File src/openvpn/crypto_epoch.c:

http://gerrit.openvpn.net/c/openvpn/+/804/comment/4fc082c6_eb80eb11 :
PS9, Line 136: 11
We could use strlen() here.

http://gerrit.openvpn.net/c/openvpn/+/804/comment/8fdb0547_fcd70ff2 :
PS9, Line 157: 8
strlen?

http://gerrit.openvpn.net/c/openvpn/+/804/comment/403bd9f7_0bc3e36d :
PS9, Line 163: 7
strlen?

http://gerrit.openvpn.net/c/openvpn/+/804/comment/769c056a_676d4fc5 :
PS9, Line 210: uint16_t current_epoch_recv = co->key_ctx_bi.decrypt.epoch; The variable name makes this look like it should be co->epoch_key_recv.epoch.

After reading the function, I think the code is correct, but together with the ASSERT below, this caused me a lot of confusion. I thought that the assert was saying that you can only generate new future keys if you used them all up.

http://gerrit.openvpn.net/c/openvpn/+/804/comment/dcaea617_c7f7124b :
PS9, Line 423: is
in?

File src/openvpn/packet_id.h:

http://gerrit.openvpn.net/c/openvpn/+/804/comment/7be792f3_fe78b9b2 :
PS9, Line 214: */
Incomplete

File src/openvpn/packet_id.c:

http://gerrit.openvpn.net/c/openvpn/+/804/comment/bf06e613_bc17423a :
PS9, Line 81: packet_id_init_recv(struct packet_id_rec *rec, int seq_backtrack, int time_backtrack, const char *name, int unit) You could make this a static function.

File tests/unit_tests/openvpn/test_crypto.c:

http://gerrit.openvpn.net/c/openvpn/+/804/comment/7799a129_e434234f :
PS9, Line 728: is uses
uses

http://gerrit.openvpn.net/c/openvpn/+/804/comment/e365ffa5_5d07eb01 :
PS9, Line 830: 1-5
2-6

http://gerrit.openvpn.net/c/openvpn/+/804/comment/c9e49297_c1a18bdd :
PS9, Line 835: assert_int_equal(epoch_lookup_decrypt_key(co, 1)->epoch, 1);
Could we make sure that it really is the retiring key by comparing it with &co->epoch_retiring_data_receive_key?

Similar for epoch 7 and &co->key_ctx_bi.decrypt

http://gerrit.openvpn.net/c/openvpn/+/804/comment/a1b07fcd_530357a4 :
PS9, Line 899: assert_null(epoch_lookup_decrypt_key(co, UINT16_MAX - 32));
This hard-codes the assumption that this function is called with a state that has num_future_keys == 32. Can't we get that number from co instead?

--
To view, visit http://gerrit.openvpn.net/c/openvpn/+/804?usp=email
To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings

Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: Id7d6a576ca8c9560cb2dfae82fc62175820e9b80
Gerrit-Change-Number: 804
Gerrit-PatchSet: 9
Gerrit-Owner: plaisthos <[email protected]...>
Gerrit-Reviewer: MaxF <[email protected]...>
Gerrit-Reviewer: flichtenheld <[email protected]...>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-Attention: plaisthos <[email protected]...>
Gerrit-Attention: flichtenheld <[email protected]...>
Gerrit-Comment-Date: Sun, 29 Dec 2024 00:42:46 +0000
Gerrit-HasComments: Yes
Gerrit-Has-Labels: Yes
Gerrit-MessageType: comment

#8 lore raw
In reply to a message not in the archive

Attention is currently required from: MaxF, flichtenheld.

plaisthos has posted comments on this change. ( http://gerrit.openvpn.net/c/openvpn/+/804?usp=email )

Change subject: Implement methods to generate and manage OpenVPN Epoch keys
......................................................................

Patch Set 9:

(23 comments)

File src/openvpn/crypto.h:

http://gerrit.openvpn.net/c/openvpn/+/804/comment/4cf4ae43_56da8da9 :
PS9, Line 175: /** the epoch of the key is if it was generated as epoch data key material */

Quoted 1 line

I think you mean: The epoch of the key, if it was generated as epoch data key material. […]

I will clarify that this only meaningful if key parameters are used in epoch data and it is not meaningful otherwise.

http://gerrit.openvpn.net/c/openvpn/+/804/comment/788a09ea_7023fafa :
PS9, Line 223: is

Quoted 1 line

this?

Acknowledged

http://gerrit.openvpn.net/c/openvpn/+/804/comment/3d1e7540_b7a53f6a :
PS9, Line 307: This limit for AEAD cipher

Quoted 1 line

The limit for AEAD ciphers?

Acknowledged

http://gerrit.openvpn.net/c/openvpn/+/804/comment/1dead943_a6393dd1 :
PS9, Line 315: key

Quoted 1 line

keys

yes but also the comment is wrong as epoch_data_keys_future only stores receive keys.

http://gerrit.openvpn.net/c/openvpn/+/804/comment/78aa125b_37d2d67c :
PS9, Line 316: to switch also sending key

Quoted 1 line

to also switch the sending key?

Acknowledged

http://gerrit.openvpn.net/c/openvpn/+/804/comment/f57d4cd6_30ec7906 :
PS9, Line 324: switch

Quoted 1 line

switched?

yes, and also before instead of bevor.

http://gerrit.openvpn.net/c/openvpn/+/804/comment/be5e1ecc_1af62691 :
PS9, Line 524: @param epoch Epoch read from packet or 0 when epoch is not used.

Quoted 1 line

That's not an argument of the function.

yes that got into a commit too early. I moved it to the commit that actually adds the epoch parameter.

File src/openvpn/crypto_epoch.h:

http://gerrit.openvpn.net/c/openvpn/+/804/comment/87827b4b_e2c66cc6 :
PS9, Line 72: * @param epoch_key Epoch key to be used
: * @param key Destination for the generated data key

Quoted 1 line

wrong order compared to the function.

Acknowledged

http://gerrit.openvpn.net/c/openvpn/+/804/comment/3e654f8c_e8b2f61e :
PS9, Line 91: using

Quoted 1 line

uses

Acknowledged

http://gerrit.openvpn.net/c/openvpn/+/804/comment/0a1c083e_f7fbf9ba :
PS9, Line 96: a

Quoted 1 line

for the receive key?

Acknowledged

http://gerrit.openvpn.net/c/openvpn/+/804/comment/f2c845b2_b82d132e :
PS9, Line 103: send_epoch_keyt

Quoted 1 line

send_epoch_key

Acknowledged

http://gerrit.openvpn.net/c/openvpn/+/804/comment/2ef5767a_589a85d0 :
PS9, Line 118: * @param e1_send The E1 send epoch key derived by TLS-EKM
: * @param e1_recv The E1 receive epoch key derived by TLS-EKM

Quoted 1 line

incomplete parameters

Acknowledged

File src/openvpn/crypto_epoch.c:

http://gerrit.openvpn.net/c/openvpn/+/804/comment/37c7c0ca_5ba66eff :
PS9, Line 136: 11

Quoted 1 line

We could use strlen() here.

I will be using sizeof(array) - 1 as strlen doesn't really like operating on a uint8_t array.

http://gerrit.openvpn.net/c/openvpn/+/804/comment/b83b22ac_c3dd00bb :
PS9, Line 157: 8

Quoted 1 line

strlen?

same as above

http://gerrit.openvpn.net/c/openvpn/+/804/comment/ae88362e_fa765dab :
PS9, Line 163: 7

Quoted 1 line

strlen?

same as above

http://gerrit.openvpn.net/c/openvpn/+/804/comment/4b15f462_6189d29b :
PS9, Line 210: uint16_t current_epoch_recv = co->key_ctx_bi.decrypt.epoch;

Quoted 1 line

The variable name makes this look like it should be co->epoch_key_recv.epoch. […]

I reworked the comment and the function doxygen to hopefully make it a bit better to understand.

http://gerrit.openvpn.net/c/openvpn/+/804/comment/85168ea8_12a0a758 :
PS9, Line 423: is

Quoted 1 line

in?

not sure how I managed to mangle this when I copy&pasted it.

File src/openvpn/packet_id.h:

http://gerrit.openvpn.net/c/openvpn/+/804/comment/929b34fb_60e60e0a :
PS9, Line 214: */

Quoted 1 line

Incomplete

Acknowledged

File src/openvpn/packet_id.c:

http://gerrit.openvpn.net/c/openvpn/+/804/comment/129a630b_4118bf6c :
PS9, Line 81: packet_id_init_recv(struct packet_id_rec *rec, int seq_backtrack, int time_backtrack, const char *name, int unit)

Quoted 1 line

You could make this a static function.

Acknowledged

File tests/unit_tests/openvpn/test_crypto.c:

http://gerrit.openvpn.net/c/openvpn/+/804/comment/b63adfb5_696e6519 :
PS9, Line 728: is uses

Quoted 1 line

uses

Done

http://gerrit.openvpn.net/c/openvpn/+/804/comment/aa4ca31d_556d7a6b :
PS9, Line 830: 1-5

Quoted 1 line

2-6

Done

http://gerrit.openvpn.net/c/openvpn/+/804/comment/cb188983_ad366b2c :
PS9, Line 835: assert_int_equal(epoch_lookup_decrypt_key(co, 1)->epoch, 1);

Quoted 1 line

Could we make sure that it really is the retiring key by comparing it with &co->epoch_retiring_data_ […]

good idea.

http://gerrit.openvpn.net/c/openvpn/+/804/comment/eca5ecc7_126d252a :
PS9, Line 899: assert_null(epoch_lookup_decrypt_key(co, UINT16_MAX - 32));

Quoted 1 line

This hard-codes the assumption that this function is called with a state that has num_future_keys == […]

I think for a unit test hardcoding this is fine. I think we want to rather hardcode it here rather then to rely on the internal state of co being correct for the unit test.

I will add an assert that the num_future_keys is 32 and then use num_future_keys as you suggested.

--
To view, visit http://gerrit.openvpn.net/c/openvpn/+/804?usp=email
To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings

Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: Id7d6a576ca8c9560cb2dfae82fc62175820e9b80
Gerrit-Change-Number: 804
Gerrit-PatchSet: 9
Gerrit-Owner: plaisthos <[email protected]...>
Gerrit-Reviewer: MaxF <[email protected]...>
Gerrit-Reviewer: flichtenheld <[email protected]...>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-Attention: flichtenheld <[email protected]...>
Gerrit-Attention: MaxF <[email protected]...>
Gerrit-Comment-Date: Sun, 29 Dec 2024 03:09:05 +0000
Gerrit-HasComments: Yes
Gerrit-Has-Labels: No
Comment-In-Reply-To: MaxF <[email protected]...>
Gerrit-MessageType: comment

#9 lore raw
In reply to a message not in the archive

Attention is currently required from: MaxF, flichtenheld.

Hello MaxF, flichtenheld,

I'd like you to reexamine a change. Please visit

    http://gerrit.openvpn.net/c/openvpn/+/804?usp=email

to look at the new patch set (#10).

The following approvals got outdated and were removed:
Code-Review-1 by MaxF

Change subject: Implement methods to generate and manage OpenVPN Epoch keys
......................................................................

Implement methods to generate and manage OpenVPN Epoch keys

This implements functions that allow these keys to be generated and managed. It does not yet implement using them for the data channel.

Change-Id Id7d6a576ca8c9560cb2dfae82fc62175820e9b80

---
M src/openvpn/crypto.c
M src/openvpn/crypto.h
M src/openvpn/crypto_epoch.c
M src/openvpn/crypto_epoch.h
M src/openvpn/packet_id.c
M src/openvpn/packet_id.h
M src/openvpn/ssl_common.h
M tests/unit_tests/openvpn/test_crypto.c

8 files changed, 806 insertions(+), 22 deletions(-)

git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/04/804/10

src/openvpn/crypto.c +21 -4
index 6e442a3..4e93882 100644
@@ -918,14 +918,27 @@
     if (cipher_ctx_mode_aead(ctx->cipher))
     {
         size_t impl_iv_len = 0;
+        size_t impl_iv_offset = 0;
         ASSERT(cipher_ctx_iv_length(ctx->cipher) >= OPENVPN_AEAD_MIN_IV_LEN);
-        impl_iv_len = cipher_ctx_iv_length(ctx->cipher) - sizeof(packet_id_type);
-        ASSERT(impl_iv_len + sizeof(packet_id_type) <= OPENVPN_MAX_IV_LENGTH);
+
+        /* Epoch keys use XOR of full IV length with the packet id to generate
+         * IVs. Old data format uses concatenation instead (XOR with 0 for the
+         * first 4 bytes (sizeof(packet_id_type) */
+        if (key->epoch)
+        {
+            impl_iv_len = cipher_ctx_iv_length(ctx->cipher);
+            impl_iv_offset = 0;
+        }
+        else
+        {
+            impl_iv_len = cipher_ctx_iv_length(ctx->cipher) - sizeof(packet_id_type);
+            impl_iv_offset = sizeof(packet_id_type);
+        }
+        ASSERT(impl_iv_offset + impl_iv_len <= OPENVPN_MAX_IV_LENGTH);
         ASSERT(impl_iv_len <= MAX_HMAC_KEY_LENGTH);
         ASSERT(impl_iv_len <= key->hmac_size);
         CLEAR(ctx->implicit_iv);
-        /* The first bytes of the IV are filled with the packet id */
-        memcpy(ctx->implicit_iv + sizeof(packet_id_type), key->hmac, impl_iv_len);
+        memcpy(ctx->implicit_iv + impl_iv_offset, key->hmac, impl_iv_len);
     }
 }
 
@@ -974,6 +987,7 @@
              hmac_ctx_size(ctx->hmac));
 
     }
+    ctx->epoch = key->epoch;
     gc_free(&gc);
 }
 
@@ -986,6 +1000,7 @@
     snprintf(log_prefix, sizeof(log_prefix), "Outgoing %s", name);
     init_key_ctx(ctx, key_params, kt, OPENVPN_OP_ENCRYPT, log_prefix);
     key_ctx_update_implicit_iv(ctx, key_params);
+    ctx->epoch = key_params->epoch;
 }
 
 void
@@ -997,6 +1012,7 @@
     snprintf(log_prefix, sizeof(log_prefix), "Incoming %s", name);
     init_key_ctx(ctx, key_params, kt, OPENVPN_OP_DECRYPT, log_prefix);
     key_ctx_update_implicit_iv(ctx, key_params);
+    ctx->epoch = key_params->epoch;
 }
 
 void
@@ -1034,6 +1050,7 @@
     }
     CLEAR(ctx->implicit_iv);
     ctx->plaintext_blocks = 0;
+    ctx->epoch = 0;
 }
 
 void
src/openvpn/crypto.h +50 -2

88 lines not on this page · raw message

src/openvpn/crypto_epoch.c +349 -0

359 lines not on this page · raw message

src/openvpn/crypto_epoch.h +91 -1

95 lines not on this page · raw message

src/openvpn/packet_id.c +34 -11

57 lines not on this page · raw message

src/openvpn/packet_id.h +7 -0
index fa7ef7c..e78d912 100644
@@ -206,6 +206,13 @@
 
 void packet_id_free(struct packet_id *p);
 
+/**
+ * Move the packet id recv structure from \c src to \c dest. \c src will will
+ * be reinitialised. \c dest will be freed before the move.
+ */
+void
+packet_id_move_recv(struct packet_id_rec *dest, struct packet_id_rec *src);
+
 /* should we accept an incoming packet id ? */
 bool packet_id_test(struct packet_id_rec *p,
                     const struct packet_id_net *pin);
src/openvpn/ssl_common.h +2 -2
index e697d68..e092472 100644
@@ -334,8 +334,8 @@
     interval_t packet_timeout;
     int64_t renegotiate_bytes;
     int64_t renegotiate_packets;
-    /** limit for AEAD cipher, this is the sum of packets + blocks
-     * that are allowed to be used */
+    /** limit for AEAD cipher when not running in epoch data key mode,
+     *  this is the sum of packets + blocks that are allowed to be used */
     uint64_t aead_usage_limit;
     interval_t renegotiate_seconds;
 
tests/unit_tests/openvpn/test_crypto.c +252 -2

275 lines not on this page · raw message

--
To view, visit http://gerrit.openvpn.net/c/openvpn/+/804?usp=email
To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings

Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: Id7d6a576ca8c9560cb2dfae82fc62175820e9b80
Gerrit-Change-Number: 804
Gerrit-PatchSet: 10
Gerrit-Owner: plaisthos <[email protected]...>
Gerrit-Reviewer: MaxF <[email protected]...>
Gerrit-Reviewer: flichtenheld <[email protected]...>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-Attention: flichtenheld <[email protected]...>
Gerrit-Attention: MaxF <[email protected]...>
Gerrit-MessageType: newpatchset

#10 lore raw
In reply to a message not in the archive

Attention is currently required from: flichtenheld, plaisthos.

MaxF has posted comments on this change. ( http://gerrit.openvpn.net/c/openvpn/+/804?usp=email )

Change subject: Implement methods to generate and manage OpenVPN Epoch keys
......................................................................

Patch Set 12: Code-Review+2

(3 comments)

Patchset:

PS12:
Looks good!

Some typos, but those can be resolved during check-in I think.

File src/openvpn/crypto_epoch.c:

http://gerrit.openvpn.net/c/openvpn/+/804/comment/b2171fc7_9bf1eb9e :
PS12, Line 215: /* We want the future receive keys to start just after the epoch of the
: * the currently used decryption key. */
"the the"

(can be fixed during commit I think)

File src/openvpn/packet_id.h:

http://gerrit.openvpn.net/c/openvpn/+/804/comment/0548db58_7fc4e970 :
PS12, Line 210: * Move the packet id recv structure from \c src to \c dest. \c src will will
: * be reinitialised. \c dest will be freed before the move.
"will will"

--
To view, visit http://gerrit.openvpn.net/c/openvpn/+/804?usp=email
To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings

Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: Id7d6a576ca8c9560cb2dfae82fc62175820e9b80
Gerrit-Change-Number: 804
Gerrit-PatchSet: 12
Gerrit-Owner: plaisthos <[email protected]...>
Gerrit-Reviewer: MaxF <[email protected]...>
Gerrit-Reviewer: flichtenheld <[email protected]...>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-Attention: plaisthos <[email protected]...>
Gerrit-Attention: flichtenheld <[email protected]...>
Gerrit-Comment-Date: Thu, 09 Jan 2025 16:33:05 +0000
Gerrit-HasComments: Yes
Gerrit-Has-Labels: Yes
Gerrit-MessageType: comment

#11 lore raw

PATCH v12 1/1 Implement methods to generate and manage OpenVPN Epoch keys

In reply to a message not in the archive

From: Arne Schwabe <[email protected]...>

This implements functions that allow these keys to be generated and managed. It does not yet implement using them for the data channel.

Change-Id Id7d6a576ca8c9560cb2dfae82fc62175820e9b80

---

This change was reviewed on Gerrit and approved by at least one developer. I request to merge it to master.

Gerrit URL: https://gerrit.openvpn.net/c/openvpn/+/804
This mail reflects revision 12 of this Change.

Acked-by according to Gerrit (reflected above):
MaxF <[email protected]...>

src/openvpn/crypto.c +21 -4
index ee9b0c6..b107f88 100644
@@ -916,14 +916,27 @@
     if (cipher_ctx_mode_aead(ctx->cipher))
     {
         size_t impl_iv_len = 0;
+        size_t impl_iv_offset = 0;
         ASSERT(cipher_ctx_iv_length(ctx->cipher) >= OPENVPN_AEAD_MIN_IV_LEN);
-        impl_iv_len = cipher_ctx_iv_length(ctx->cipher) - sizeof(packet_id_type);
-        ASSERT(impl_iv_len + sizeof(packet_id_type) <= OPENVPN_MAX_IV_LENGTH);
+
+        /* Epoch keys use XOR of full IV length with the packet id to generate
+         * IVs. Old data format uses concatenation instead (XOR with 0 for the
+         * first 4 bytes (sizeof(packet_id_type) */
+        if (key->epoch)
+        {
+            impl_iv_len = cipher_ctx_iv_length(ctx->cipher);
+            impl_iv_offset = 0;
+        }
+        else
+        {
+            impl_iv_len = cipher_ctx_iv_length(ctx->cipher) - sizeof(packet_id_type);
+            impl_iv_offset = sizeof(packet_id_type);
+        }
+        ASSERT(impl_iv_offset + impl_iv_len <= OPENVPN_MAX_IV_LENGTH);
         ASSERT(impl_iv_len <= MAX_HMAC_KEY_LENGTH);
         ASSERT(impl_iv_len <= key->hmac_size);
         CLEAR(ctx->implicit_iv);
-        /* The first bytes of the IV are filled with the packet id */
-        memcpy(ctx->implicit_iv + sizeof(packet_id_type), key->hmac, impl_iv_len);
+        memcpy(ctx->implicit_iv + impl_iv_offset, key->hmac, impl_iv_len);
     }
 }
 
@@ -972,6 +985,7 @@
              hmac_ctx_size(ctx->hmac));
 
     }
+    ctx->epoch = key->epoch;
     gc_free(&gc);
 }
 
@@ -984,6 +998,7 @@
     snprintf(log_prefix, sizeof(log_prefix), "Outgoing %s", name);
     init_key_ctx(ctx, key_params, kt, OPENVPN_OP_ENCRYPT, log_prefix);
     key_ctx_update_implicit_iv(ctx, key_params);
+    ctx->epoch = key_params->epoch;
 }
 
 void
@@ -995,6 +1010,7 @@
     snprintf(log_prefix, sizeof(log_prefix), "Incoming %s", name);
     init_key_ctx(ctx, key_params, kt, OPENVPN_OP_DECRYPT, log_prefix);
     key_ctx_update_implicit_iv(ctx, key_params);
+    ctx->epoch = key_params->epoch;
 }
 
 void
@@ -1032,6 +1048,7 @@
     }
     CLEAR(ctx->implicit_iv);
     ctx->plaintext_blocks = 0;
+    ctx->epoch = 0;
 }
 
 void
src/openvpn/crypto.h +50 -2

88 lines not on this page · raw message

src/openvpn/crypto_epoch.c +349 -0

359 lines not on this page · raw message

src/openvpn/crypto_epoch.h +91 -1

95 lines not on this page · raw message

src/openvpn/packet_id.c +34 -11

57 lines not on this page · raw message

src/openvpn/packet_id.h +7 -0
index c05ac9a..9c0f464 100644
@@ -206,6 +206,13 @@
 
 void packet_id_free(struct packet_id *p);
 
+/**
+ * Move the packet id recv structure from \c src to \c dest. \c src will will
+ * be reinitialised. \c dest will be freed before the move.
+ */
+void
+packet_id_move_recv(struct packet_id_rec *dest, struct packet_id_rec *src);
+
 /* should we accept an incoming packet id ? */
 bool packet_id_test(struct packet_id_rec *p,
                     const struct packet_id_net *pin);
src/openvpn/ssl_common.h +2 -2
index e697d68..e092472 100644
@@ -334,8 +334,8 @@
     interval_t packet_timeout;
     int64_t renegotiate_bytes;
     int64_t renegotiate_packets;
-    /** limit for AEAD cipher, this is the sum of packets + blocks
-     * that are allowed to be used */
+    /** limit for AEAD cipher when not running in epoch data key mode,
+     *  this is the sum of packets + blocks that are allowed to be used */
     uint64_t aead_usage_limit;
     interval_t renegotiate_seconds;
 
tests/unit_tests/openvpn/test_crypto.c +252 -2

275 lines not on this page · raw message

#12 lore raw

[Openvpn-devel] [PATCH applied] Re: Implement methods to generate and manage OpenVPN Epoch keys

Another patch in the series that doesn't actually change anything yet, but builds infrastructure. It comes with a fairly extensive unit test, which passes :-)

The changes look reasonable to me, and have a +2 from MaxF in Gerrit (who understands crypto much better).

As the patch actually does add a (not yet used) branch to the regular AEAD cipher path and also changes the "impl_iv_offset" stuff, I stared a bit harder, and subjected everything to the client/server testbed - and nothing breaks in the old packet format.

Two comments have been grammar fixed as instructed ("the the -> the" and "will will -> will").

Your patch has been applied to the master branch.

commit 92adbc88b1b37095cebde2a1c5b6ae242f382678

Author: Arne Schwabe
Date:   Thu Jan 9 19:05:37 2025 +0100
     Implement methods to generate and manage OpenVPN Epoch keys

     Signed-off-by: Arne Schwabe <[email protected]...>
     Acked-by: MaxF <[email protected]...>
     Message-Id: <[email protected]...>
     URL: https://www.mail-archive.com/[email protected]/msg30390.html
     Signed-off-by: Gert Doering <[email protected]...>

--
kind regards,

Gert Doering

#13 lore raw
In reply to a message not in the archive

cron2 has uploaded a new patch set (#13) to the change originally created by plaisthos. ( http://gerrit.openvpn.net/c/openvpn/+/804?usp=email )

The following approvals got outdated and were removed:
Code-Review+2 by MaxF

Change subject: Implement methods to generate and manage OpenVPN Epoch keys
......................................................................

Implement methods to generate and manage OpenVPN Epoch keys

This implements functions that allow these keys to be generated and managed. It does not yet implement using them for the data channel.

Change-Id Id7d6a576ca8c9560cb2dfae82fc62175820e9b80

Message-Id: <[email protected]...>
URL: https://www.mail-archive.com/[email protected]/msg30390.html

---
M src/openvpn/crypto.c
M src/openvpn/crypto.h
M src/openvpn/crypto_epoch.c
M src/openvpn/crypto_epoch.h
M src/openvpn/packet_id.c
M src/openvpn/packet_id.h
M src/openvpn/ssl_common.h
M tests/unit_tests/openvpn/test_crypto.c

8 files changed, 806 insertions(+), 22 deletions(-)

git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/04/804/13

src/openvpn/crypto.c +21 -4
index ee9b0c6..b107f88 100644
@@ -916,14 +916,27 @@
     if (cipher_ctx_mode_aead(ctx->cipher))
     {
         size_t impl_iv_len = 0;
+        size_t impl_iv_offset = 0;
         ASSERT(cipher_ctx_iv_length(ctx->cipher) >= OPENVPN_AEAD_MIN_IV_LEN);
-        impl_iv_len = cipher_ctx_iv_length(ctx->cipher) - sizeof(packet_id_type);
-        ASSERT(impl_iv_len + sizeof(packet_id_type) <= OPENVPN_MAX_IV_LENGTH);
+
+        /* Epoch keys use XOR of full IV length with the packet id to generate
+         * IVs. Old data format uses concatenation instead (XOR with 0 for the
+         * first 4 bytes (sizeof(packet_id_type) */
+        if (key->epoch)
+        {
+            impl_iv_len = cipher_ctx_iv_length(ctx->cipher);
+            impl_iv_offset = 0;
+        }
+        else
+        {
+            impl_iv_len = cipher_ctx_iv_length(ctx->cipher) - sizeof(packet_id_type);
+            impl_iv_offset = sizeof(packet_id_type);
+        }
+        ASSERT(impl_iv_offset + impl_iv_len <= OPENVPN_MAX_IV_LENGTH);
         ASSERT(impl_iv_len <= MAX_HMAC_KEY_LENGTH);
         ASSERT(impl_iv_len <= key->hmac_size);
         CLEAR(ctx->implicit_iv);
-        /* The first bytes of the IV are filled with the packet id */
-        memcpy(ctx->implicit_iv + sizeof(packet_id_type), key->hmac, impl_iv_len);
+        memcpy(ctx->implicit_iv + impl_iv_offset, key->hmac, impl_iv_len);
     }
 }
 
@@ -972,6 +985,7 @@
              hmac_ctx_size(ctx->hmac));
 
     }
+    ctx->epoch = key->epoch;
     gc_free(&gc);
 }
 
@@ -984,6 +998,7 @@
     snprintf(log_prefix, sizeof(log_prefix), "Outgoing %s", name);
     init_key_ctx(ctx, key_params, kt, OPENVPN_OP_ENCRYPT, log_prefix);
     key_ctx_update_implicit_iv(ctx, key_params);
+    ctx->epoch = key_params->epoch;
 }
 
 void
@@ -995,6 +1010,7 @@
     snprintf(log_prefix, sizeof(log_prefix), "Incoming %s", name);
     init_key_ctx(ctx, key_params, kt, OPENVPN_OP_DECRYPT, log_prefix);
     key_ctx_update_implicit_iv(ctx, key_params);
+    ctx->epoch = key_params->epoch;
 }
 
 void
@@ -1032,6 +1048,7 @@
     }
     CLEAR(ctx->implicit_iv);
     ctx->plaintext_blocks = 0;
+    ctx->epoch = 0;
 }
 
 void
src/openvpn/crypto.h +50 -2

88 lines not on this page · raw message

src/openvpn/crypto_epoch.c +349 -0

359 lines not on this page · raw message

src/openvpn/crypto_epoch.h +91 -1

95 lines not on this page · raw message

src/openvpn/packet_id.c +34 -11

57 lines not on this page · raw message

src/openvpn/packet_id.h +7 -0
index c05ac9a..07e92ff 100644
@@ -206,6 +206,13 @@
 
 void packet_id_free(struct packet_id *p);
 
+/**
+ * Move the packet id recv structure from \c src to \c dest. \c src will
+ * be reinitialised. \c dest will be freed before the move.
+ */
+void
+packet_id_move_recv(struct packet_id_rec *dest, struct packet_id_rec *src);
+
 /* should we accept an incoming packet id ? */
 bool packet_id_test(struct packet_id_rec *p,
                     const struct packet_id_net *pin);
src/openvpn/ssl_common.h +2 -2
index e697d68..e092472 100644
@@ -334,8 +334,8 @@
     interval_t packet_timeout;
     int64_t renegotiate_bytes;
     int64_t renegotiate_packets;
-    /** limit for AEAD cipher, this is the sum of packets + blocks
-     * that are allowed to be used */
+    /** limit for AEAD cipher when not running in epoch data key mode,
+     *  this is the sum of packets + blocks that are allowed to be used */
     uint64_t aead_usage_limit;
     interval_t renegotiate_seconds;
 
tests/unit_tests/openvpn/test_crypto.c +252 -2

275 lines not on this page · raw message

--
To view, visit http://gerrit.openvpn.net/c/openvpn/+/804?usp=email
To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings

Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: Id7d6a576ca8c9560cb2dfae82fc62175820e9b80
Gerrit-Change-Number: 804
Gerrit-PatchSet: 13
Gerrit-Owner: plaisthos <[email protected]...>
Gerrit-Reviewer: MaxF <[email protected]...>
Gerrit-Reviewer: flichtenheld <[email protected]...>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-MessageType: newpatchset

#14 lore raw
In reply to a message not in the archive

cron2 has submitted this change. ( http://gerrit.openvpn.net/c/openvpn/+/804?usp=email )

Change subject: Implement methods to generate and manage OpenVPN Epoch keys
......................................................................

Implement methods to generate and manage OpenVPN Epoch keys

This implements functions that allow these keys to be generated and managed. It does not yet implement using them for the data channel.

Change-Id Id7d6a576ca8c9560cb2dfae82fc62175820e9b80

Message-Id: <[email protected]...>
URL: https://www.mail-archive.com/[email protected]/msg30390.html

---
M src/openvpn/crypto.c
M src/openvpn/crypto.h
M src/openvpn/crypto_epoch.c
M src/openvpn/crypto_epoch.h
M src/openvpn/packet_id.c
M src/openvpn/packet_id.h
M src/openvpn/ssl_common.h
M tests/unit_tests/openvpn/test_crypto.c

8 files changed, 806 insertions(+), 22 deletions(-)
src/openvpn/crypto.c +21 -4
index ee9b0c6..b107f88 100644
@@ -916,14 +916,27 @@
     if (cipher_ctx_mode_aead(ctx->cipher))
     {
         size_t impl_iv_len = 0;
+        size_t impl_iv_offset = 0;
         ASSERT(cipher_ctx_iv_length(ctx->cipher) >= OPENVPN_AEAD_MIN_IV_LEN);
-        impl_iv_len = cipher_ctx_iv_length(ctx->cipher) - sizeof(packet_id_type);
-        ASSERT(impl_iv_len + sizeof(packet_id_type) <= OPENVPN_MAX_IV_LENGTH);
+
+        /* Epoch keys use XOR of full IV length with the packet id to generate
+         * IVs. Old data format uses concatenation instead (XOR with 0 for the
+         * first 4 bytes (sizeof(packet_id_type) */
+        if (key->epoch)
+        {
+            impl_iv_len = cipher_ctx_iv_length(ctx->cipher);
+            impl_iv_offset = 0;
+        }
+        else
+        {
+            impl_iv_len = cipher_ctx_iv_length(ctx->cipher) - sizeof(packet_id_type);
+            impl_iv_offset = sizeof(packet_id_type);
+        }
+        ASSERT(impl_iv_offset + impl_iv_len <= OPENVPN_MAX_IV_LENGTH);
         ASSERT(impl_iv_len <= MAX_HMAC_KEY_LENGTH);
         ASSERT(impl_iv_len <= key->hmac_size);
         CLEAR(ctx->implicit_iv);
-        /* The first bytes of the IV are filled with the packet id */
-        memcpy(ctx->implicit_iv + sizeof(packet_id_type), key->hmac, impl_iv_len);
+        memcpy(ctx->implicit_iv + impl_iv_offset, key->hmac, impl_iv_len);
     }
 }
 
@@ -972,6 +985,7 @@
              hmac_ctx_size(ctx->hmac));
 
     }
+    ctx->epoch = key->epoch;
     gc_free(&gc);
 }
 
@@ -984,6 +998,7 @@
     snprintf(log_prefix, sizeof(log_prefix), "Outgoing %s", name);
     init_key_ctx(ctx, key_params, kt, OPENVPN_OP_ENCRYPT, log_prefix);
     key_ctx_update_implicit_iv(ctx, key_params);
+    ctx->epoch = key_params->epoch;
 }
 
 void
@@ -995,6 +1010,7 @@
     snprintf(log_prefix, sizeof(log_prefix), "Incoming %s", name);
     init_key_ctx(ctx, key_params, kt, OPENVPN_OP_DECRYPT, log_prefix);
     key_ctx_update_implicit_iv(ctx, key_params);
+    ctx->epoch = key_params->epoch;
 }
 
 void
@@ -1032,6 +1048,7 @@
     }
     CLEAR(ctx->implicit_iv);
     ctx->plaintext_blocks = 0;
+    ctx->epoch = 0;
 }
 
 void
src/openvpn/crypto.h +50 -2

88 lines not on this page · raw message

src/openvpn/crypto_epoch.c +349 -0

359 lines not on this page · raw message

src/openvpn/crypto_epoch.h +91 -1

95 lines not on this page · raw message

src/openvpn/packet_id.c +34 -11

57 lines not on this page · raw message

src/openvpn/packet_id.h +7 -0
index c05ac9a..07e92ff 100644
@@ -206,6 +206,13 @@
 
 void packet_id_free(struct packet_id *p);
 
+/**
+ * Move the packet id recv structure from \c src to \c dest. \c src will
+ * be reinitialised. \c dest will be freed before the move.
+ */
+void
+packet_id_move_recv(struct packet_id_rec *dest, struct packet_id_rec *src);
+
 /* should we accept an incoming packet id ? */
 bool packet_id_test(struct packet_id_rec *p,
                     const struct packet_id_net *pin);
src/openvpn/ssl_common.h +2 -2
index e697d68..e092472 100644
@@ -334,8 +334,8 @@
     interval_t packet_timeout;
     int64_t renegotiate_bytes;
     int64_t renegotiate_packets;
-    /** limit for AEAD cipher, this is the sum of packets + blocks
-     * that are allowed to be used */
+    /** limit for AEAD cipher when not running in epoch data key mode,
+     *  this is the sum of packets + blocks that are allowed to be used */
     uint64_t aead_usage_limit;
     interval_t renegotiate_seconds;
 
tests/unit_tests/openvpn/test_crypto.c +252 -2

275 lines not on this page · raw message

--
To view, visit http://gerrit.openvpn.net/c/openvpn/+/804?usp=email
To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings

Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: Id7d6a576ca8c9560cb2dfae82fc62175820e9b80
Gerrit-Change-Number: 804
Gerrit-PatchSet: 13
Gerrit-Owner: plaisthos <[email protected]...>
Gerrit-Reviewer: MaxF <[email protected]...>
Gerrit-Reviewer: flichtenheld <[email protected]...>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-MessageType: merged